package edu.ntu.cloudroid.client;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import edu.ntu.cloudroid.model.IDs;
import edu.ntu.cloudroid.model.UserData;
import edu.ntu.cloudroid.model.data.DetachedData;
import edu.ntu.cloudroid.model.data.LocationInfo;
import edu.ntu.cloudroid.model.data.StatInfo;
import edu.ntu.cloudroid.model.data.TraceInfo;
import edu.ntu.cloudroid.model.data.VitalSignData;
import edu.ntu.cloudroid.model.data.VitalSignInfo;
import edu.ntu.cloudroid.model.friend.FriendInfo;
import edu.ntu.cloudroid.model.friend.GroupInfo;
import edu.ntu.cloudroid.model.policy.PolicyInfo;
import edu.ntu.cloudroid.model.profile.ProfileInfo;
import edu.ntu.cloudroid.nongui.com.AbstractResponse;
import edu.ntu.cloudroid.nongui.com.Communicator;
import edu.ntu.cloudroid.nongui.com.LocationRequestWithInputs;
import edu.ntu.cloudroid.nongui.com.MetaDataRequest;
import edu.ntu.cloudroid.nongui.com.RequestNoInputs;
import edu.ntu.cloudroid.nongui.com.VitalDataRequestWithInputs;
import edu.ntu.cloudroid.nongui.com.VitalRequestWithFilteringFunction;

public class TestRequest {
	public static void main(String[] args) throws Exception {
		Communicator com = new Communicator(null, "localhost", 8080);
		getData(com);
		/*MetaDataRequest mdr = new MetaDataRequest("user_0@gmail.com",
				MetaDataRequest.Friend);
		UserData ud = com.getMetaData(mdr);
		FriendInfo friend = (FriendInfo)ud;
		System.out.println("Friend list: ");
		for (String s : friend.getFriendList()){
			System.out.print(s+"  ");
		}
		System.out.println();
		System.out.println("Group list: ");
		for (String s: friend.getGroupList())
			System.out.print(s+"  ");
		System.out.println();
				
		mdr = new MetaDataRequest("user_0@gmail.com", MetaDataRequest.Policy);
		PolicyInfo pi = (PolicyInfo)com.getMetaData(mdr);
		System.out.println(pi.getUserEmail());
		for (String s: pi.getPreferenceString())
			System.out.print(s+" , ");
		System.out.println();
				
		GroupInfo gi = null;
		try {
			mdr = new MetaDataRequest("user_0@gmail.com",
					"user_0@gmail.com_group_0", MetaDataRequest.Group);
			gi = (GroupInfo) com.getMetaData(mdr);
			System.out.println(gi.getGroupName());
			for (String s : gi.getMembers())
				System.out.print(s + "  ");
			System.out.println();
			for (String s : gi.getGroupPolicy().getPreferenceString())
				System.out.print(s + "   ");
			System.out.println();
		} catch (Exception e) {
			System.out.println("Failed to retrieve group meta data");
		}*/
				
				
		//Deleting				
		/*gi = new GroupInfo("user_0@gmail.com",
				"user_0@gmail.com_group_0", null, GroupInfo.REMOVE);
		com.sendData(gi);*/
				
		//Update policies
		/*List<PolicyInfo> policies = Generator.generatePolicies(1, 10, false);
		for (PolicyInfo policyInfo : policies)
			com.sendData(policyInfo);
				
		FriendInfo fi = new FriendInfo("user_0@gmail.com", "user_9@gmail.com",
				FriendInfo.ADD);
		com.sendData(fi);*/
		
	}
	
	private static void getData(Communicator com) {
		// RequestNoInputs lr = new RequestNoInputs("user_0@gmail.com");
		// LocationRequestWithInputs lr = new LocationRequestWithInputs(
		// "user_0@gmail.com", 103.681959, 1.342976, "");

		/*List<PolicyInfo> policies = Generator.generatePolicies(0, 10, false);
		VitalRequestWithFilteringFunction vrf = new VitalRequestWithFilteringFunction(
				"user_0@gmail.com", policies.get(0));				
		AbstractResponse r = com.getData(vrf);
		System.out.println("Decision = " + r.getAccessDecision());
		if (!r.getAccessDecision())
			return;

		Map<String, Map<String, List<DetachedData>>> locs = r.getReturnedData();

		Iterator<String> it = locs.keySet().iterator();
		Map<String, List<DetachedData>> li = null;
		VitalSignInfo locInfo = null;
		String policyInfo;
		while (it.hasNext()) {
			String uid = it.next();
			System.out.println("friend " + uid + ": ");
			li = (Map<String, List<DetachedData>>) locs.get(uid);
			Iterator<String> it1 = li.keySet().iterator();
			while (it1.hasNext()) {
				policyInfo = it1.next();
				List<DetachedData> list = li.get(policyInfo);
				for (DetachedData dd : list) {
					if (dd instanceof StatInfo) {
						System.out.println("policy Info: " + policyInfo
								+ " aggregate = "
								+ ((StatInfo) dd).getAggregate());
						break;
					}
					locInfo = (VitalSignInfo) dd;
					if (locInfo.isRangeResult())
						System.out.println("policy Info:  " + policyInfo
								+ " data: [" + locInfo.getLowHR() + " - "
								+ locInfo.getHighHR() + "] , ["
								+ locInfo.getLowOV() + " - "
								+ locInfo.getHighOV() + "]");
					else
						System.out.println("policy Info:  " + policyInfo
								+ " data: " + locInfo.getHeartRate() + " , "
								+ locInfo.getOxygenVolume());
				}
			}
		}*/
	}
}
